Beginning Programming - Part Eleven

Appendix

© 2005, Brad Moore

Liberty Basic Connection

Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index


Appendix A:


'Pong - By Brad Moore
'Placed into the public domain April 2005
'Use 'p' and 'l' to move the paddle

   'We need a few parameters before we begin
   xchg = -10
   ychg = ((int(rnd(0)*8)-3) / (int(rnd(0)*3)+1))
   xloc = 440
   yloc = 80
   paddleloc = 85
   paddlesize = 60
   paddle = 0
   b$ = "##"

    NOMAINWIN
    WindowWidth = 515 : WindowHeight = 303
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)

graphicbox  #main.gfx, 10, 10, 480, 210
button      #main.go, "Go!",[go],UL, 385, 230, 105, 25
statictext  #main.st1, "Speed: nn", 10, 233, 100, 25
statictext  #main.st3, "Y-change: nn", 160, 233, 200, 25

Open "Window Title" for Window as #main
    #main "trapclose [quit]"
[start_over]
    #main.gfx "down; fill White; flush"
    #main "font ms_sans_serif 10"
    'create the paddle at the starting location
    #main.gfx "color white; backcolor red"
    #main.gfx "up; goto 465 ";paddleloc;"; down; boxfilled 475 ";paddleloc+paddlesize
    'trap the character input 
    #main.gfx "when characterInput [fetch]"

[loop]
    Wait

[quit]
    close #main : END

[go]
    'set the focus to the graphicbox
    #main.gfx "setfocus"
    #main.gfx "color white"
    timer 50 , [move]
    wait
    
    
[fetch]
    'process user input
    a$ = Inkey$
    a$ = upper$(a$)
    
    'when program begins, b$ is preset to default "##"
    if b$ = "##" then b$ = a$
    'if the last keypress was P and this is P then increase down movement
    if a$ = "P" and b$ = "P" then
        'move paddle up
        paddle = paddle - 3
        if paddle < -9 then paddle = -9
        b$ = a$
        goto [move]
    end if
    
    'if the last keypress was L and this is L then increase the upward movement
    if a$ = "L" and b$ = "L" then
        'move paddle down
        paddle = paddle + 3
        if paddle > 9 then paddle = 9
        b$ = a$
        goto [move]
    end if
    
    'This keypress is not the same as the last - stop the paddle
    paddle = 0
    b$ = "##"
    goto [move] 
    
    
[move]
    'process the paddle movement
    oldpaddleloc = paddleloc - 5
    paddleloc = paddleloc + paddle
    if paddleloc < -10 then paddleloc = -10
    if paddleloc > 190 then paddleloc = 190

    'draw the paddle
    #main.gfx "backcolor white"
    #main.gfx "up; goto 465 ";oldpaddleloc;"; down; boxfilled 475 ";oldpaddleloc+paddlesize+10
    #main.gfx "backcolor red"
    #main.gfx "up; goto 465 ";paddleloc;"; down; boxfilled 475 ";paddleloc+paddlesize    

    #main.st1 "Speed: " + str$(abs(xchg))
    #main.st3 "Y-change: " + str$(abs(ychg))
    oldx = xloc
    oldy = yloc
    xloc = xloc + xchg
    yloc = yloc + ychg
    'there is a weird bug that occasionally occurs where y can keep getting smaller
    'or it can keep getting bigger - fix it here
    if yloc < 0 then yloc = 0
    if yloc > 210 then yloc = 210
    #main.gfx "backcolor white"
    #main.gfx "up; goto ";oldx;" ";int(oldy);"; down; circlefilled 10"
    #main.gfx "backcolor red"
    #main.gfx "up; goto ";xloc;" ";int(yloc);"; down; circlefilled 8"

    'check to see if we should have hit the paddle
    if xloc > 466 then 
        'see if we hit that paddle or missed
        if yloc > paddleloc - 5 and yloc < paddleloc + paddlesize + 5  then
            'we hit the paddle - time to rebound...
            goto [changedirection]
        else
            confirm "You missed the ball - game over!" + chr$(13) + _
                    "Would you like to play again?";answer$
            if answer$ = "no" then
                goto [quit]
            else
                ballspeed = 2
                xchg = -10
                ychg = ((int(rnd(0)*10)-3) / (int(rnd(0)*3)+1))
                xloc = 440
                yloc = 80
                paddleloc = 85
                paddle = 0
                b$ = "##"
                #main.gfx "cls"
                goto [start_over]
            end if
        end if
    end if
            
    if yloc < 2 or yloc > 208 then 
        ychg = ychg * -1
    end if
    if xloc < -2 or xloc > 482 then [changedirection]
    wait


[changedirection]

    'adjust the ball speed (x direction of travel)
    ballspeed = int(rnd(0)*4)-1
    direction = -1
    if xchg < 0 then direction = 1
    xchg = abs(xchg)    
    xchg = xchg + ballspeed
    if xchg < 8 then xchg = 8
    if xchg > 26 then xchg = 26
    xchg = xchg * direction

    'adjust the ball's defelction angle (y direction of travel)
    direction = 1
    if ychg < 0 then direction = -1
    ychg = abs(ychg)
    ychg = ychg + ((int(rnd(0)*9)-3) / (int(rnd(0)*3)+1))
    if ychg < 0 then ychg = 0
    if ychg > 10 then ychg = 11.25
    ychg = ychg * direction
    
    wait                                                       

Appendix B


'CherryPong - By Brad Moore
'Placed into the public domain April 2005
'Use 'p' and 'l' to move the paddle
'This program requires graphic elements downloadable
'from the Liberty Basic Newsletter website in issue 132

   'We need a few parameters before we begin
   xchg = -10
   ychg = ((int(rnd(0)*8)-3) / (int(rnd(0)*3)+1))
   xloc = 430
   yloc = 80
   paddleloc = 85
   paddlesize = 60
   paddle = 0
   b$ = "##"
   
   'load the bitmaps
   loadbmp "c0", "cherry0.bmp" 
   loadbmp "c1", "cherry1.bmp" 
   loadbmp "c2", "cherry2.bmp" 
   loadbmp "c3", "cherry3.bmp" 
   loadbmp "c4", "cherry4.bmp" 
   loadbmp "c5", "cherry5.bmp" 
   loadbmp "c6", "cherry6.bmp" 
   loadbmp "c7", "cherry7.bmp" 
   loadbmp "pdl", "paddle.bmp"
   loadbmp "bg", "bkgnd.bmp" 


    NOMAINWIN
    WindowWidth = 515 : WindowHeight = 303
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)

graphicbox  #main.gfx, 10, 10, 480, 210
button      #main.go, "Go!",[go],UL, 385, 230, 105, 25
statictext  #main.st1, "Speed: nn", 10, 233, 100, 25
statictext  #main.st3, "Y-change: nn", 160, 233, 200, 25

Open "Window Title" for Window as #main
    #main "trapclose [quit]"
    #main.gfx "down; fill White; flush"
    #main "font ms_sans_serif 10"
    'create the sprites
    #main.gfx "addsprite ball c0 c1 c2 c3 c4 c5 c6 c7" 
    #main.gfx "addsprite paddle pdl" 
    'add a background
    #main.gfx "background bg"
[start_over]    
    'place the sprites
    #main.gfx "spritexy paddle 465 ";paddleloc
    #main.gfx "spritexy ball 800 800"
    'make the background visible
    #main.gfx "drawsprites"
    'cycle the ball sprite
    #main.gfx "cyclesprite ball 1"
    'make the sprite round
    #main.gfx "spriteround ball"
    'trap the character input 
    #main.gfx "when characterInput [fetch]"

[loop]
    Wait

[quit]
    close #main : END

[go]
    'set the focus to the graphicbox
    #main.gfx "setfocus"
    timer 50 , [move]
    wait
    
    
[fetch]
    'process user input
    a$ = Inkey$
    a$ = upper$(a$)
    
    'when program begins, b$ is preset to default "##"
    if b$ = "##" then b$ = a$
    'if the last keypress was P and this is P then increase down movement
    if a$ = "P" and b$ = "P" then
        'move paddle up
        paddle = paddle - 3
        if paddle < -9 then paddle = -9
        b$ = a$
        goto [move]
    end if
    
    'if the last keypress was L and this is L then increase the upward movement
    if a$ = "L" and b$ = "L" then
        'move paddle down
        paddle = paddle + 3
        if paddle > 9 then paddle = 9
        b$ = a$
        goto [move]
    end if
    
    'This keypress is not the same as the last - stop the paddle
    paddle = 0
    b$ = "##"
    goto [move] 
    
    
[move]
    'process the actual paddle movement and prevent it from exceeding limits.
    paddleloc = paddleloc + paddle
    if paddleloc < -10 then paddleloc = -10
    if paddleloc > 190 then paddleloc = 190

    #main.gfx "spritexy paddle 465 ";paddleloc

    #main.st1 "Speed: " + str$(abs(xchg))
    #main.st3 "Y-change: " + str$(abs(ychg))
    
    'process the ball movement
    xloc = xloc + xchg
    yloc = yloc + ychg

    'there is a weird bug that occasionally occurs where y can keep getting smaller
    'or it can keep getting bigger - fix it here
    if yloc < -10 then yloc = -10
    if yloc > 200 then yloc = 200

    'place the ball sprite where we want it to be
    #main.gfx "spritexy ball ";xloc;" ";int(yloc)
    'draw the sprites now
    #main.gfx "drawsprites"
    
    #main.gfx "spritecollides paddle list$"
    if instr(list$, "ball") > 0 then
        'we hit the paddle - time to rebound...
        goto [changedirection]
    end if

    'check to see if we should have hit the paddle
    if xloc > 450 then 
        goto [quitting]
    end if
            
    if yloc < -8 or yloc > 196 then 
        ychg = ychg * -1
    end if
    if xloc < -2 or xloc > 482 then [changedirection]
    wait


[changedirection]

    'adjust the ball speed (x direction of travel)
    ballspeed = int(rnd(0)*4)-1
    direction = -1
    if xchg < 0 then direction = 1
    xchg = abs(xchg)    
    xchg = xchg + ballspeed
    if xchg < 8 then xchg = 8
    if xchg > 26 then xchg = 26
    xchg = xchg * direction

    'adjust the ball's defelction angle (y direction of travel)
    direction = 1
    if ychg < 0 then direction = -1
    ychg = abs(ychg)
    ychg = ychg + ((int(rnd(0)*9)-3) / (int(rnd(0)*3)+1))
    if ychg < 0 then ychg = 0
    if ychg > 10 then ychg = 11.25
    ychg = ychg * direction
    
    wait     
    

[quitting]  
    timer 0
    confirm "You missed the ball - game over!" + chr$(13) + _
            "Would you like to play again?";answer$
    if answer$ = "no" then
        goto [quit]
    else
        ballspeed = 2
        xchg = -10
        ychg = ((int(rnd(0)*10)-3) / (int(rnd(0)*3)+1))
        xloc = 430
        yloc = 80
        paddleloc = 85
        paddle = 0
        b$ = "##"
        #main.go "!setfocus"
        goto [start_over]
    end if


Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index